import random

from redis import ConnectionPool, StrictRedis


class RedisClient(object):
    pool = ConnectionPool(**{
        'host': '127.0.0.1',
        'port': 6379,
        'db': 0,
        'password': 950218
    })
    client = StrictRedis(connection_pool=pool)

    def GetTask(self):
        taskId = self.client.rpop('local_task')
        if taskId:
            return str(taskId, encoding='utf-8')
        else:
            return None

    def GetPreInternetTask(self):
        taskId = self.client.rpop('pre_internet_task')
        if taskId:
            return str(taskId, encoding='utf-8')
        else:
            return None

    def GetInternetTask(self):
        taskId = self.client.rpop('internet_task')
        if taskId:
            return str(taskId, encoding='utf-8')
        else:
            return None

    def RecoverKeyWords(self, order_number, keywords):
        self.client.lpush(f'keywords_internet_{order_number}', keywords)

    def CheckTaskStatus(self,task):
        taskId = self.client.hget('tasks_status', task)
        if taskId:
            return int(taskId)
        else:
            return None

    def UpdateTaskStatus(self, task, status):
        self.client.hset('tasks_status', task, status)

    def DeleteTaskStatus(self, task):
        self.client.hdel('tasks_status', task)

    def GetInternetTaskKeyWords(self, order_number):
        taskId = self.client.lpop(f'keywords_internet_{order_number}')
        if taskId:
            return str(taskId, encoding='utf-8')
        else:
            return None

    def reConnect(self):
        try:
            self.client.ping()
        except:
            self.client = StrictRedis(connection_pool=self.pool)

    def set_cookies(self, cookies):
        self.reConnect()
        self.client.set('baidu_cookies', cookies)

    def get_cookies(self):
        self.reConnect()
        return str(self.client.get('baidu_cookies'), encoding='utf-8')

    def set_referer(self, url):
        self.reConnect()
        self.client.set('referer', url)

    def get_referer(self):
        self.reConnect()
        return str(self.client.get('referer'), encoding='utf-8')

    def push_suggest_sentence(self, data):
        self.reConnect()
        self.client.rpush('suggest', data)

    def get_suggest_sentence(self):
        self.reConnect()
        sentence = self.client.lpop('suggest')
        if sentence:
            return str(sentence, encoding='utf-8')
        else:
            return None



class RedisClientAdsl(object):
    pool = ConnectionPool(**{
        'host': '39.105.230.139',
        'port': 6379,
        'db': 5,
        'password': 950218
    })
    client = StrictRedis(connection_pool=pool)

    def reConnect(self):
        try:
            self.client.ping()
        except:
            self.client = StrictRedis(connection_pool=self.pool)

    def ADSL(self):
        self.reConnect()
        key = 'adsl'
        try:
            data = self.client.hvals(key)
            if data is None:
                return None
            else:
                ip = random.choice(data)
                return str(ip.decode())
        except:
            return None

if __name__ == '__main__':
    r = RedisClient()
    print(r.CheckTaskStatus('11322'))
